import {Storage} from 'src/utils/storage.js'

let coupon = Storage.get('coupon')
if (coupon) {
  coupon = JSON.parse(coupon)
} else {
  coupon = {}
}

const state = {
  coupon,
  friendId: Storage.get('friendId') || ''
}

const actions = {
  shareCoup({commit}, param) {
    commit('share-coup', param)
  }
}
const getters = {
  coupon: state => state.coupon,
  friendId: state => state.friendId
}

const mutations = {
  shareCoup(state, param) {
    if (param) {
      Storage.set('coupon', JSON.stringify(param))
      state.coupon = JSON.parse(Storage.get('coupon'))
    } else {
      Storage.remove('coupon')
      state.coupon = {}
    }
  },
  shareCoupUser(state, {friendId}) {
    if (friendId) {
      Storage.set('friendId', friendId)
      state.friendId = Storage.get('friendId')
    } else {
      Storage.remove('friendId')
      state.friendId = ''
    }
  }
}

export default {
  state,
  actions,
  getters,
  mutations
}
